/** * Create by mzq * Description: 编辑患者个人信息 * Date: 2025/2/13 15:41 *
Update: 2025/2/13 15:41 */

<template>
  <MyDiaLog
    :visible="value"
    :title-name="'编辑患者信息'"
    :width="600"
    @close="handleClose">
    <template>
      <div class="patient-info-content">
        <uni-forms
          :model="formData"
          ref="patientForm"
          label-width="100px">
          <uni-forms-item label="病人姓名:" name="name" label-align="right">
            <uni-easyinput
              v-model="formData.name"
              placeholder="请输入姓名"
              :styles="inputStyles" />
          </uni-forms-item>
          <uni-forms-item label="性别:" name="gender" label-align="right">
            <uni-data-select
              v-model="formData.gender"
              :localdata="genderOptions"
              placeholder="请选择性别"
              :clear="false" />
          </uni-forms-item>
          <uni-forms-item label="年龄:" name="age" label-align="right">
            <uni-easyinput
              v-model="formData.age"
              type="number"
              placeholder="请输入年龄"
              :styles="inputStyles" />
          </uni-forms-item>
          <uni-forms-item label="住院号:" name="hospitalNo" label-align="right">
            <uni-easyinput
              v-model="formData.hospitalNo"
              placeholder="请输入住院号"
              :styles="inputStyles" />
          </uni-forms-item>
          <uni-forms-item label="住院科室:" name="department" label-align="right">
            <uni-easyinput
              v-model="formData.department"
              placeholder="请输入住院科室"
              :styles="inputStyles" />
          </uni-forms-item>
          <uni-forms-item label="初步诊断:" name="diagnosis" label-align="right">
            <uni-easyinput
              v-model="formData.diagnosis"
              placeholder="请输入初步诊断"
              :styles="textareaStyles" />
          </uni-forms-item>
        </uni-forms>
        <view class="button-group">
          <div>
            <button class="uni-btn" @click="handleClose">取消</button>
          </div>
          <div>
            <button class="uni-btn uni-btn-primary" @click="handleSubmit">确定</button>
          </div>
        </view>
      </div>
    </template>
  </MyDiaLog>
</template>

<script>
import MyDiaLog from '@/components/MyDiaLog/MyDiaLog.vue'
import UniFormsItem from '@/uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue'
import UniDataSelect from '@/uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue'
import UniEasyinput from '@/uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue'
import { updateJsonById } from '@/utils/fileUtil'

export default {
  name: 'EditInformation',
  components: {
    MyDiaLog,
    UniFormsItem,
    UniDataSelect,
    UniEasyinput
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    patientInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      formData: {
        name: '',
        gender: '',
        age: '',
        hospitalNo: '',
        department: '',
        diagnosis: ''
      },
      genderOptions: [
        { value: '男', text: '男' },
        { value: '女', text: '女' }
      ],
      inputStyles: {
        border: '1px solid #dcdfe6',
        borderRadius: '4px',
        padding: '0 10px'
      },
      textareaStyles: {
        border: '1px solid #dcdfe6',
        borderRadius: '4px',
        padding: '5px 10px',
        minHeight: '80px'
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('input', false)
    },

    initPatientInfo() {
      console.log('patientInfo:', this.patientInfo)
      if (this.patientInfo) {
        this.formData = {
          name: this.patientInfo.name || '',
          gender: this.patientInfo.sex || '',
          age: this.patientInfo.age || '',
          hospitalNo: this.patientInfo.admissionNumber || '',
          department: this.patientInfo.Department || '',
          diagnosis: this.patientInfo.diagnosis || ''
        }
      }
    },

    validateForm() {
      if (!this.formData.name) {
        uni.showToast({
          title: '请输入病人姓名',
          icon: 'none'
        })
        return false
      }
      if (!this.formData.gender) {
        uni.showToast({
          title: '请选择性别',
          icon: 'none'
        })
        return false
      }
      if (!this.formData.age) {
        uni.showToast({
          title: '请输入年龄',
          icon: 'none'
        })
        return false
      }
      if (!this.formData.hospitalNo) {
        uni.showToast({
          title: '请输入住院号',
          icon: 'none'
        })
        return false
      }
      if (!this.formData.department) {
        uni.showToast({
          title: '请输入住院科室',
          icon: 'none'
        })
        return false
      }
      if (!this.formData.diagnosis) {
        uni.showToast({
          title: '请输入初步诊断',
          icon: 'none'
        })
        return false
      }
      return true
    },

    handleSubmit() {
      if (this.validateForm()) {
        const updateData = {
          name: this.formData.name,
          sex: this.formData.gender,
          age: this.formData.age,
          admissionNumber: this.formData.hospitalNo,
          Department: this.formData.department,
          diagnosis: this.formData.diagnosis,
          operator: this.patientInfo.operator || 'sysadmin'
        }

        // 检查是否需要更新文件
        let success = true;
        if (this.patientInfo.shouldUpdateFile) {
          console.log('需要更新患者报告文件');
          // 使用患者ID命名的报告文件路径
          const patientReportFile = `/sdcard/test/${this.patientInfo.id}.json`;
          
          success = updateJsonById(
            this.patientInfo.id,
            updateData,
            patientReportFile
          );
          
          if (!success) {
            uni.showToast({
              title: '更新文件失败',
              icon: 'none'
            });
            return;
          }
        } else {
          console.log('仅更新Vuex中的患者信息，不更新文件');
        }

        // 更新Vuex中的患者信息
        try {
          // 获取Vuex store
          const store = this.$store;
          if (store && store.state.currentPatient) {
            // 更新Vuex中的患者信息
            const updatedPatient = {
              ...store.state.currentPatient,
              ...updateData
            };
            store.commit('setCurrentPatient', updatedPatient);
            console.log('已更新Vuex中的患者信息:', updatedPatient);
          }
          
          // 更新当前组件的patientInfo
          Object.assign(this.patientInfo, updateData);
        } catch (error) {
          console.error('更新Vuex中的患者信息失败:', error);
        }

        uni.showToast({
          title: '更新成功',
          icon: 'success'
        });
        this.$emit('update-success');
        this.handleClose();
      }
    }
  },
  watch: {
    value(newVal) {
      if (newVal) {
        this.initPatientInfo()
      }
    },
    patientInfo: {
      handler(newVal) {
        if (newVal) {
          this.initPatientInfo()
        }
      },
      immediate: true
    }
  }
}
</script>

<style scoped lang="scss">
.patient-info-content {
  padding: 20px;

  /deep/ .uni-forms-item {
    margin-bottom: 22px;

    .uni-forms-item__label {
      font-size: 14px;
      text-align: right !important;
      padding-right: 12px;
      box-sizing: border-box;
      width: 100px;
      justify-content: flex-end;
      display: flex;
    }
  }

  .button-group {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 10px;
    text-align: center;

    .uni-btn {
      margin: 0 10px;
      padding: 0 20px;
      height: 35px;
      line-height: 35px;
      font-size: 14px;
      border-radius: 4px;
      color: #000000;
      border: none;
      background: #E9E9E9;

      &.uni-btn-primary {
        background: #6DF7BE;
        border: none;
      }
    }
  }
}
</style>
